package com.manager.commons;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.TimeZone;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * Copyrights 1997-2008 西安协同时光软件有限公司<br/>
 * 
 * Filename:StringUtil<br/>
 * 
 * Description:主要用于MD5算法的实现,使用java的MessageDigest<br/>
 * 
 * Version:V0.1<br/>
 * 
 * Author: peigen<br/>
 * 
 * History: <br/> [ Author Date Version Content ]<br/> peigen 2008-08-19 V0.1
 * create <br/>
 * 
 */
public class StringUtil {
	private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

	/**
	 * 转换字节数组为16进制字串
	 * 
	 * @param b
	 *            字节数组
	 * @return 16进制字串
	 */
	public static String byteArrayToHexString(byte[] b) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			resultSb.append(byteToHexString(b[i]));
		}
		return resultSb.toString();
	}

	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n = 256 + n;
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	public static String MD5Encode(String origin) {
		String resultString = null;
		if (origin != null && !origin.equals("")) {
			try {
				resultString = new String(origin);
				MessageDigest md = MessageDigest.getInstance("MD5");
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes()));
			} catch (Exception ex) {
			}
		}
		return resultString;
	}

	/**
	 * @description 字符串日期型转化为yyyy-MM-dd日期型
	 * 
	 * @param strDate
	 * 
	 * @return 转化后的日期型
	 */
	public static Date stringToDate(String strDate) throws Exception {

		// 日期格式化
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		// 强制专为日期型
		Date cDate = df.parse(strDate);

		return cDate;

	}

	/**
	 * @description 字符串日期型转化为yyyy-MM-dd HH:mm:ss日期型
	 * 
	 * @param strDate
	 * 
	 * @return 转化后的日期型
	 */
	public static Date stringToDateTime(String strDate) throws Exception {

		// 日期格式化
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		// 强制专为日期型
		Date cDate = df.parse(strDate);

		return cDate;

	}

	/**
	 * @description 日期型转化为yyyy-MM-dd字符串
	 * 
	 * @param date
	 * 
	 * @return 转化后的字符串型
	 */
	public static String dateToString(Date date) throws Exception {

		// 日期格式化
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		// 强制专为日期型
		String strDate = df.format(date);

		return strDate;

	}

	/**
	 * @description 日期型转化为yyyy-MM-dd HH:mm:ss字符串
	 * 
	 * @param date
	 * 
	 * @return 转化后的字符串型
	 */
	public static String dateTimeToString(Date date) throws Exception {

		// 日期格式化
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		// 强制专为日期型
		String strDate = df.format(date);

		return strDate;

	}

	/**
	 * 获取当前日期时间格式如：yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */

	public static String getCurrDatetime() {
		// 处理时间不对
		TimeZone tz = TimeZone.getTimeZone("ETC/GMT-8");
		TimeZone.setDefault(tz);
		Date now = new Date();
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s = outFormat.format(now);
		return s;
	}

	/**
	 * @description 星期转换
	 * 
	 * @param int
	 * 
	 * @return string
	 */
	private static String convert(int val) {
		String retStr = "";
		switch (val) {
		case 0:
			return "星期日";
		case 1:
			return "星期一";
		case 2:
			return "星期二";
		case 3:
			return "星期三";
		case 4:
			return "星期四";
		case 5:
			return "星期五";
		case 6:
			return "星期六";
		default:
			break;
		}
		return retStr;
	}

	/**
	 * 获得当前日期格式为yyyy年mm月dd星期
	 * 
	 * @return
	 */
	public static String displayCurrentDateAndWeek() {
		Calendar cal = Calendar.getInstance(); // 创建一个日历对象。
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DATE);
		String week = convert(cal.get(Calendar.DAY_OF_WEEK) - 1);
		String currentDate = year + "年" + month + "月" + day + "日" + "    "
				+ week;
		return currentDate;
	}

	/**
	 * 获得当前日期格式为yyyy-mm-dd
	 * 
	 * @return
	 */
	public static String displayCurrentDate() {
		Calendar cal = Calendar.getInstance(); // 创建一个日历对象。
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DATE);
		String week = convert(cal.get(Calendar.DAY_OF_WEEK) - 1);
		String currentDate = year + "-" + month + "-" + day;
		return currentDate;
	}

	// 对字符串编码转换
	public static String stringChineseTrans(HttpServletRequest request,
			String str) {
		String strMethod = request.getMethod();
		if (!"POST".equals(strMethod)) {
			try {
				if (str != null)
					str = new String(str.getBytes("ISO-8859-1"), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}

		}
		return str;
	}

	/**
	 * 将数组转化为字符串
	 * 
	 * @param strMarry
	 * @return String
	 */
	public static String changArrayToString(String[] strMarry, String flag) {

		String str = "";
		String temp = "";
		String tempFlag = "";
		if (strMarry == null) {
			return "";
		}
		for (int i = 0; i < strMarry.length; i++) {
			temp = strMarry[i];
			if (i == 0) {
				tempFlag = "";
			} else {
				tempFlag = flag;
			}
			str = str + tempFlag + temp;
		}
		return str;
	}

	public static void main(String[] args) {
		String a[] = new String[] { "2", "3", "4" };
		StringUtil.changArrayToString(a, ",");

	}

	/**
	 * @author shiyu TODO 判断对象是不是null，如果是返回""
	 * 
	 * @param obj
	 * @return
	 */
	public static String sNull(Object obj) {
		// 当obj==null时，如果使用toString()，将会出错！
		return obj == null ? "" : obj.toString();
	}

	/**
	 * @author shiyu TODO 用于字符转换
	 * 
	 * @param str
	 * @return
	 */
	public static String getStr(String str) {
		try {
			if (sNull(str).equals(""))
				return "";
			String temp_p = sNull(str);
			byte[] temp_t = temp_p.getBytes("ISO8859-1");
			// String temp = new String(temp_t, "gb2312");
			String temp = new String(temp_t, "GBK");
			temp = temp.replaceAll("\"", "'");
			temp = temp.replaceAll("'", "''");
			// temp = temp_p.replaceAll("'","''");

			return temp;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获得6位随即数字
	 * @return
	 */
	public static String getRandomNun(){
		Random r = new Random();
		String sj = "";
		for(int i=0;i<6;i++){
			sj = sj+r.nextInt(9); 
		}
		return sj;
		
	}

}
